//
//  NSAttributedString+ZTEmotion.m
//  AFNetworking
//
//  Created by 武得亮 on 2021/3/12.
//

#import "NSAttributedString+ZTEmotion.h"
//#import <YYText.h>
//#import <YYImage.h>

#define kEmotionTopMargin -3.0f



//@implementation KZTextAttachment
//
//
//- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex
//{
////    return CGRectMake( 0, kEmotionTopMargin, lineFrag.size.height, lineFrag.size.height);
//    return CGRectMake( 0 , -5, 18, 18);
//}
//@end

@implementation NSAttributedString (ZTEmotion)

//----------------------------------------------------------------------实例方法----------------------------------------------------------------------
/*
 * 返回绘制NSAttributedString所需要的区域
 */
- (CGRect)boundsWithSize:(CGSize)size
{
    CGRect contentRect = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    return contentRect;
}

//----------------------------------------------------------------------静态方法----------------------------------------------------------------------
/*
 * 返回表情数组
 */
+ (NSArray *)emojiStringArray
{
    return [NSArray arrayWithObjects:@"[大哭]",@"[足球]",@"[发怒]",@"[挥手]",@"[猪头]",@"[傲慢]",@"[流汗]",@"[流汗]",@"[爱心]",@"[冷汗]",@"[拳头]",@"[飞吻]",@"[发抖]",@"[美女]",@"[难过]",@"[色]",@"[撇嘴]",@"[玫瑰]",@"[咖啡]",@"[偷笑]",@"[差劲]",@"[微笑]",@"[爱你]",@"[抓狂]",@"[擦汗]",@"[再见]",@"[月亮]",@"[示爱]",@"[西瓜]",@"[阴险]",@"[快哭了]",@"[疑问]",@"[亲亲]",@"[可爱]",@"[敲打]",@"[骷髅]",@"[饥饿]",@"[奋斗]",@"[跳绳]",@"[闭嘴]",@"[乒乓球]",@"[太阳]",@"[飘虫]",@"[发呆]",@"[右哼哼]",@"[坏笑]",@"[凋谢]",@"[怄火]",@"[惊讶]",@"[困]",@"[惊恐]",@"[白眼]",@"[得意]",@"[街舞]",@"[刀]",@"[鼓掌]",@"[害羞]",@"[礼物]",@"[大兵]",@"[流泪]",@"[吐]",@"[啤酒]",@"[磕头]",@"[糗大了]",@"[NO]",@"[右太极]",@"[便便]",@"[咒骂]",@"[睡]",@"[爱情]",@"[胜利]",@"[菜刀]",@"[蛋糕]",@"[勾引]",@"[委屈]",@"[钱]",@"[篮球]",@"[嘘]",@"[衰]",@"[抠鼻]",@"[献吻]",@"[心碎]",@"[OK]",@"[强]",@"[弱]",@"[憨笑]",@"[左哼哼]",@"[晕]",@"[调皮]",@"[酷]",@"[呲牙]",@"[激动]",@"[跳跳]",@"[握手]",@"[鄙视]",@"[抱拳]",@"[吓]",@"[回头]",@"[尴尬]",@"[炸弹]",@"[折磨]",@"[转圈]",@"[可怜]",@"[闪电]",@"[拥抱]",@"[饭]",@"[左太极]",@"[哈欠]",nil];
}

/*
 * 返回绘制文本需要的区域
 */
+ (CGRect)boundsForString:(NSString *)string size:(CGSize)size attributes:(NSDictionary *)attrs
{
    NSAttributedString *attributedString = [NSAttributedString emotionAttributedStringFrom:string attributes:attrs];
    CGRect contentRect = [attributedString boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    return contentRect;
}

/*
 * 返回Emotion替换过的 NSAttributedString
 */

+ (NSAttributedString *)emotionAttributedStringFrom:(NSString *)string attributes:(NSDictionary *)attrs
{
    NSMutableAttributedString *attributedEmotionString = [[NSMutableAttributedString alloc] initWithString:string attributes:attrs];
    
//    NSArray *attachmentArray = [NSAttributedString attachmentsForAttributedString:attributedEmotionString];
//    for (KZTextAttachment *attachment in attachmentArray)
//    {
//        NSAttributedString *emotionAttachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
//        [attributedEmotionString replaceCharactersInRange:attachment.range withAttributedString:emotionAttachmentString];
//    }
    return attributedEmotionString;
}

/*
 * 查找所有表情文本并替换
 */
+ (NSArray *)attachmentsForAttributedString:(NSMutableAttributedString *)attributedString
{
    NSString *markL       = @"[";
    NSString *markR       = @"]";
    NSString *string      = attributedString.string;
    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *stack = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < string.length; i++)
    {
        NSString *s = [string substringWithRange:NSMakeRange(i, 1)];
        
        if (([s isEqualToString:markL]) || ((stack.count > 0) && [stack[0] isEqualToString:markL]))
        {
            if (([s isEqualToString:markL]) && ((stack.count > 0) && [stack[0] isEqualToString:markL]))
            {
                [stack removeAllObjects];
            }
            
            [stack addObject:s];
            
            if ([s isEqualToString:markR] || (i == string.length - 1))
            {
                NSMutableString *emojiStr = [[NSMutableString alloc] init];
                for (NSString *c in stack)
                {
                    [emojiStr appendString:c];
                }
                
                if ([[NSAttributedString emojiStringArray] containsObject:emojiStr])
                {
                    NSRange range = NSMakeRange(i + 1 - emojiStr.length, emojiStr.length);
                    
                    [attributedString replaceCharactersInRange:range withString:@" "];
//                    KZTextAttachment *attachment = [[KZTextAttachment alloc] initWithData:nil ofType:nil];
//                    attachment.range = NSMakeRange(i + 1 - emojiStr.length, 1);
//                    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"face" ofType:@"plist"];
//                    NSDictionary *faceDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
//                    attachment.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[faceDic objectForKey:emojiStr]]];
//                    attachment.imageName = emojiStr;
//                    i -= ([stack count] - 1);
//                    [array addObject:attachment];
                }
                [stack removeAllObjects];
            }
        }
    }
    return array;
}


+ (NSMutableAttributedString *)crazy_emotionAttributedStringFrom:(NSMutableAttributedString *)string attributes:(NSDictionary *)attrs
{
    //    NSMutableAttributedString *attributedEmotionString = [[NSMutableAttributedString alloc] initWithString:string attributes:attrs];
    NSMutableAttributedString *attributedEmotionString = string;
    NSArray *attachmentArray = [NSAttributedString crazy_attachmentsForAttributedString:attributedEmotionString];
//    for (KZTextAttachment *attachment in attachmentArray)
//    {
//        //        NSAttributedString *emotionAttachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
//        [attributedEmotionString replaceCharactersInRange:attachment.range withAttributedString:attachment.attachText];
//    }
    return attributedEmotionString;
}

+ (NSString *)noEmotion_crazy_emotionAttributedStringFrom:(NSMutableAttributedString *)string attributes:(NSDictionary *)attrs
{
    //    NSMutableAttributedString *attributedEmotionString = [[NSMutableAttributedString alloc] initWithString:string attributes:attrs];
    NSMutableAttributedString *attributedEmotionString = string;
    NSMutableAttributedString *attributedEmotionString2 = string;
    NSArray *attachmentArray = [NSAttributedString crazy_attachmentsForAttributedString:attributedEmotionString];
//    for (KZTextAttachment *attachment in attachmentArray)
//    {
//        //        NSAttributedString *emotionAttachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
//        [attributedEmotionString replaceCharactersInRange:attachment.range withAttributedString:attachment.attachText];
//        [attributedEmotionString2 replaceCharactersInRange:attachment.range withString:@" "];
//    }
    return [attributedEmotionString2 string];
}

/*
 * 查找所有表情文本并替换
 */
+ (NSArray *)crazy_attachmentsForAttributedString:(NSMutableAttributedString *)attributedString
{
    NSString *markL       = @"[";
    NSString *markR       = @"]";
    NSString *string      = attributedString.string;
    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *stack = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < string.length; i++)
    {
        NSString *s = [string substringWithRange:NSMakeRange(i, 1)];
        
        if (([s isEqualToString:markL]) || ((stack.count > 0) && [stack[0] isEqualToString:markL]))
        {
            if (([s isEqualToString:markL]) && ((stack.count > 0) && [stack[0] isEqualToString:markL]))
            {
                [stack removeAllObjects];
            }
            
            [stack addObject:s];
            
            if ([s isEqualToString:markR] || (i == string.length - 1))
            {
                NSMutableString *emojiStr = [[NSMutableString alloc] init];
                for (NSString *c in stack)
                {
                    [emojiStr appendString:c];
                }
                
                if ([[NSAttributedString emojiStringArray] containsObject:emojiStr])
                {
                    NSRange range = NSMakeRange(i + 1 - emojiStr.length, emojiStr.length);
                    
                    [attributedString replaceCharactersInRange:range withString:@" "];
//                    KZTextAttachment *attachment = [[KZTextAttachment alloc] initWithData:nil ofType:nil];
//                    attachment.range = NSMakeRange(i + 1 - emojiStr.length, 1);
//                    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"face" ofType:@"plist"];
//                    NSDictionary *faceDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
//                    attachment.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[faceDic objectForKey:emojiStr]]];
//                    attachment.imageName = emojiStr;
//                    i -= ([stack count] - 1);
                    
//                    YYAnimatedImageView *imageView1= [[YYAnimatedImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[faceDic objectForKey:emojiStr]]]];
//                    NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView1 contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(18, 18) alignToFont:[UIFont systemFontOfSize:15] alignment:YYTextVerticalAlignmentCenter];
//                    attachment.attachText = attachText;
//
//                    [array addObject:attachment];
                }
                [stack removeAllObjects];
            }
        }
    }
    return array;
}

/*
 * 查找所有表情文本并替换 用来计算高度
 */
+ (NSArray *)crazy_attachmentsForAttributedString1:(NSMutableAttributedString *)attributedString
{
    NSString *markL       = @"[";
    NSString *markR       = @"]";
    NSString *string      = attributedString.string;
    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *stack = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < string.length; i++)
    {
        NSString *s = [string substringWithRange:NSMakeRange(i, 1)];
        
        if (([s isEqualToString:markL]) || ((stack.count > 0) && [stack[0] isEqualToString:markL]))
        {
            if (([s isEqualToString:markL]) && ((stack.count > 0) && [stack[0] isEqualToString:markL]))
            {
                [stack removeAllObjects];
            }
            
            [stack addObject:s];
            
            if ([s isEqualToString:markR] || (i == string.length - 1))
            {
                NSMutableString *emojiStr = [[NSMutableString alloc] init];
                for (NSString *c in stack)
                {
                    [emojiStr appendString:c];
                }
                
                if ([[NSAttributedString emojiStringArray] containsObject:emojiStr])
                {
                    NSRange range = NSMakeRange(i + 1 - emojiStr.length, emojiStr.length);
                    
                    [attributedString replaceCharactersInRange:range withString:@" "];
//                    KZTextAttachment *attachment = [[KZTextAttachment alloc] initWithData:nil ofType:nil];
//                    attachment.range = NSMakeRange(i + 1 - emojiStr.length, 1);
//                    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"face" ofType:@"plist"];
//                    NSDictionary *faceDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
//                    attachment.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[faceDic objectForKey:emojiStr]]];
//                    attachment.imageName = emojiStr;
//                    i -= ([stack count] - 1);
                    
//                    YYAnimatedImageView *imageView1= [[YYAnimatedImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[faceDic objectForKey:emojiStr]]]];
//                    NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView1 contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(18, 18) alignToFont:[UIFont systemFontOfSize:15] alignment:YYTextVerticalAlignmentCenter];
//                    attachment.attachText = attachText;
                    
//                    [array addObject:attachment];
                }
                [stack removeAllObjects];
            }
        }
    }
    return array;
}


@end
